package httpserver;

import java.io.*;
import java.net.*;
import java.util.concurrent.*;
public class Server {

	private int port = 8080;
	private ServerSocket serverSocket;
	private ExecutorService executorService;
	private final int poolsize = 30;
	public int number = 0;
	
	//instruct function
	public Server() throws IOException
	{
		serverSocket = new ServerSocket(port);
		//executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*poolsize);
		executorService = Executors.newCachedThreadPool();
		System.out.println("The server starts");
		
	}
	
	//support service
	public void service()
	{
		while(true)
		{
			Socket socket = null;
			try{
				socket = serverSocket.accept();
				number++;
				System.out.println(number);
				executorService.execute(new Hanlder(socket));
			}catch(Exception e)
			{
				System.out.println(e.getMessage());
			}
		}
	}
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		try{
		new Server().service();
		}catch(Exception exception)
		{
		   System.out.println(exception.getMessage());
		}
		
	}

}
